package com.ccmusic.dao.impl;

import java.util.List;

import com.ccmusic.dao.ListViewDao;
import com.ccmusic.entity.ListView;
import com.ccmusic.entity.PersonalList;

public class ListViewDaoImpl extends BaseDao implements ListViewDao {

	@Override
	public boolean creatList(int uid, String lname, String lcover) {
		// 步骤一：新建歌单，填写歌单基本信息
		String sql = "insert into personallist values(0,?,?,?)";
		int n1 = this.execute(sql, uid, lname, lcover);

		// 在新歌单中添加默认歌曲，首先要获取新歌单的lid，下面提供获取新歌单lid的方法
		String sql1 = "select * from personallist where uid=? and lname=?";
		List<PersonalList> list = (List<PersonalList>) this.select(sql1, PersonalList.class, uid, lname);
		int newLid = list.get(0).getLid();

		// 步骤二：新增一首默认歌曲lid=newLid & mid=0到listmatching中去
		String sql2 = "insert into listmatching values(?,?)";
		int n2 = this.execute(sql2, newLid, 0);
		return (n1 > 0 && n2 > 0) ? true : false;
	}

	@Override
	public boolean addToList(String lid, String mid) {
		String sql = "insert into listmatching values(0,?,?,?)";
		return false;
	}

	@Override
	public List<ListView> getLists(String uid) {
		String sql = "select * from listview where uid=?";
		List<ListView> list = (List<ListView>) this.select(sql, ListView.class, uid);
		return list;
	}

	@Override
	public boolean queryListByUidAndLname(String uid, String lname) {
		String sql = "select * from personallist where uid=? and lname=?";
		List<PersonalList> list = (List<PersonalList>) this.select(sql, PersonalList.class, uid, lname);
		return (list != null) ? true : false;
	}

	@Override
	public boolean delList(String uid, String lname) {
		String sql = "delete from personallist where uid=? and lname=?";
		int n = this.execute(sql, uid, lname);
		return n > 0 ? true : false;
	}

}
